Ext.define("jwawa.erp.framework.HandlerUtil", {
   initRef: function(view, ref){
       var me = this;

       for(var r in ref){
           if(typeof(ref[r]) === 'string'){
               me[r] = view.down(ref[r]);
//               me[r] = Ext.ComponentQuery.query(ref[r])[0];
           }else if(typeof(ref[r]) === 'function'){
               me[r] = ref[r].call(this);
           }
       }
   },

    initResource: function(){
        var me = this;

        if(!Ext.isEmpty(me.resources)){
            if(Ext.isArray(me.resources)){
                Ext.each(me.resources, function(resource){
                    me.addResource(resource['name'], resource['res'], resource['type'], resource['cfg']);
                })
            } else {
                Ext.Object.each(me.resources, function(key, value) {
                    me.addResource(key, value, 'object', null);
                });
            }
        }
    },

    initEflMapping: function(){
        var me = this,
            elfMapping = Ext.JwawaResMgr.get(EFL_MAPPING),
            entityArray = [];

        if(Ext.isArray(me.entities) && me.entities.length > 0){
            Ext.each(me.entities, function(entityName) {
                if(Ext.isEmpty(elfMapping[entityName])){
                    entityArray.push(entityName)
                }
            });
        }

        if(entityArray.length > 0){
            var entityDetailModel = Ext.JwawaResMgr.get(ENTITY_DETAIL_MODEL);
            var tmpEflMapping = entityDetailModel.getEntityMapByNames(entityArray);
            Ext.apply(elfMapping, tmpEflMapping);
        }
    },

    addResource: function(resName, res, type, cfg){
        var me = this,
            resObj = null;

        var regResObj = Ext.JwawaResMgr.get(resName);
        if(!Ext.isEmpty(regResObj)){
            return regResObj;
        }
        if(Ext.isEmpty(type)){
            type = 'object'
        }

        if(Ext.isString(res) || Ext.isEmpty(res)){
            switch(type){
                case 'object':
                    resObj = Ext.create(res);
                    break;
                case 'store':
                    var storeClass = res ? res : 'Ext.data.Store';
                    var storeCfg = cfg ? cfg : {fields: []};
                    resObj = Ext.create(storeClass, storeCfg);
                    break;
            }
        }else if(Ext.isObject(res)){
            resObj = res;
        } else {
            resObj = {}
        }

        return Ext.JwawaResMgr.register(resName, resObj);
    }
});